package com.ruicar.afs.cloud.afscase.loanmoderuleinfo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruicar.afs.cloud.afscase.loanmoderuleinfo.condition.LoanModeRuleCondition;
import com.ruicar.afs.cloud.afscase.loanmoderuleinfo.entity.LoanModeRuleInfo;
import com.ruicar.afs.cloud.afscase.loanmoderuleinfo.service.LoanModeRuleInfoService;
import com.ruicar.afs.cloud.afscase.mq.approvesendinfo.service.ApproveLoanInfoService;
import com.ruicar.afs.cloud.common.core.query.QueryCondition;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.modules.afscorebusiness.enums.WhetherEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * @author panhj
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/loanModeRule")
@Api("放款模式规则配置")
public class LoanModeRuleController {
    private final LoanModeRuleInfoService loanModeRuleInfoService;
    private final ApproveLoanInfoService approveLoanInfoService;

    /**
     * 获取GPS安装规则配置数据
     *
     * @param queryCondition
     * @return
     */
    @PostMapping(value = "/getModeRuleList")
    @ApiOperation(value = "多条件分页获取放款模式规则配置数据")
    public IResponse<IPage<LoanModeRuleInfo>> getGpsRuleList(@RequestBody QueryCondition<LoanModeRuleCondition> queryCondition) {
        LoanModeRuleCondition condition = queryCondition.getCondition();
        return IResponse.success(loanModeRuleInfoService.page(new Page(condition.getPageNumber(), condition.getPageSize()), Wrappers.<LoanModeRuleInfo>query().lambda()
                .eq(StringUtils.isNotEmpty(condition.getIsEnable()), LoanModeRuleInfo::getIsEnable, condition.getIsEnable())
                .like(StringUtils.isNotEmpty(condition.getLendingMode()), LoanModeRuleInfo::getLendingMode, condition.getLendingMode())
                .like(StringUtils.isNotEmpty(condition.getRuleDesc()), LoanModeRuleInfo::getRuleDesc, condition.getRuleDesc())
                .eq(null != condition.getEffectTime(), LoanModeRuleInfo::getEffectTime, condition.getEffectTime())
                .eq(null != condition.getFailureTime(), LoanModeRuleInfo::getFailureTime, condition.getFailureTime())));
    }

    /**
     * 新增放款模式规则配置数据
     *
     * @param loanModeRuleInfo
     * @return
     */
    @PostMapping(value = "/addRule")
    @ApiOperation(value = "新增放款暂复核则成功")
    public IResponse<Boolean> addRule(@RequestBody LoanModeRuleInfo loanModeRuleInfo) {
        loanModeRuleInfoService.save(loanModeRuleInfo);
        return new IResponse<Boolean>().setMsg("新增放款模式规则成功！");
    }


    /**
     * 编辑放款模式规则配置数据
     *
     * @param loanModeRuleInfo
     * @return
     */
    @PostMapping(value = "/editRule")
    @ApiOperation(value = "修改放款模式规则成功")
    public IResponse<Boolean> editRule(@RequestBody LoanModeRuleInfo loanModeRuleInfo) {
        loanModeRuleInfoService.updateById(loanModeRuleInfo);
        return new IResponse<Boolean>().setMsg("修改放款模式规则成功！");
    }

    /**
     * 更新规则表业务id
     *
     * @param loanModeRuleInfo
     * @return
     */
    @PostMapping(value = "/updateRuleId")
    @ApiOperation(value = "更新规则表业务id")
    public IResponse<Boolean> updateRuleId(@RequestBody LoanModeRuleInfo loanModeRuleInfo) {
        //重置同步状态
        loanModeRuleInfo.setIsSynchronous(WhetherEnum.NO.getCode());
        loanModeRuleInfoService.updateById(loanModeRuleInfo);
        return new IResponse<Boolean>().setMsg("保存成功，规则已生效！");
    }

    /**
     * 启用放款模式规则数据
     *
     * @param id
     * @return
     */
    @PostMapping(value = "/openRuleById/{id}")
    @ApiOperation(value = "启用放款模式规则数据")
    public IResponse<Boolean> openRuleById(@PathVariable Long id) throws ParseException {
        loanModeRuleInfoService.activeRule(id);
        return new IResponse<Boolean>().setMsg("启用放款模式规则成功！");
    }

    /**
     * 停用放款模式规则数据
     *
     * @param id
     * @return
     */
    @PostMapping(value = "/closeRuleById/{id}")
    @ApiOperation(value = "停用放款模式规则数据")
    public IResponse<Boolean> closeRuleById(@PathVariable Long id) throws ParseException {
        loanModeRuleInfoService.deActiveRule(id);
        return new IResponse<Boolean>().setMsg("停用放款模式规则成功！");
    }


    /**
     * 同步放款模式规则数据
     *
     * @param id
     * @return
     */
    @PostMapping(value = "/synchronousById/{id}")
    @ApiOperation(value = "停用放款模式规则数据")
    public IResponse<Boolean> synchronousById(@PathVariable Long id) {
        //更新放款模式同步信息
        loanModeRuleInfoService.updateGpsRulesInfo(id);
        //推送放款模式信息到进件系统
        approveLoanInfoService.sendLoanModelToApply(id);
        return new IResponse<Boolean>().setMsg("放款模式信息推送成功！");
    }
}
